package com.example.customeview;

import android.graphics.Matrix;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;


/**
 * @author Caleb_F
 * @description: 不带双手缩放
 * @date :2020-09-18 15:25
 */
public class PictureActivity extends AppCompatActivity {
    private ImageView myImageview;
    private GestureDetector gestureDetector;

    private Matrix changeMatrix = new Matrix();
    private Matrix savedMatrix = new Matrix();
    private Matrix initMatix = new Matrix();

    private PointF startPoint = new PointF();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture);
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        myImageview = (ImageView) findViewById(R.id.my_imageview);
        myImageview.setOnTouchListener(new MyTouchListener());
        gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
            private Matrix doubleTapMatirx = new Matrix();
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                doubleTapMatirx.postScale(1.2f, 1.2f, e.getX(), e.getY());
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        myImageview.setImageMatrix(doubleTapMatirx);
                    }
                }, 200);
                return true;
            }
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        myImageview.setImageMatrix(initMatix);
                    }
                }, 200);
                return true;
            }
        });

    }

    class MyTouchListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;
            //保存初始状态
            initMatix = myImageview.getMatrix();
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    startPoint.set(event.getX(), event.getY());
                    break;
                case MotionEvent.ACTION_MOVE:
                        changeMatrix.set(savedMatrix);
                        changeMatrix.postTranslate(event.getX() - startPoint.x, 0);
                    break;
                default:
                    break;
            }
            view.setImageMatrix(changeMatrix);
            return true;
        }

    }

}
